package com.texplose.service.impl;

import com.texplose.entity.enums.AppHttpCodeEnum;
import com.texplose.entity.vo.ResponseResult;
import com.texplose.exception.SystemException;
import com.texplose.mapper.CollectionMapper;
import com.texplose.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : lyj
 * @Timer : 2022/5/23
 * @Description :
 */
@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;


    @Override
    public void deleteCollectionById(Integer id) {
        collectionMapper.deleteCollectionById(id);
    }

    @Override
    public void addCollection(Integer userId, Integer recipeId) {
        Boolean aBoolean = collectionMapper.checkCollection(userId, recipeId);

        if (aBoolean != null || Boolean.TRUE.equals(aBoolean)) {
            throw new SystemException(AppHttpCodeEnum.COLLECTION_IS_EXISTS);
        } else if (aBoolean== null || !aBoolean){
            collectionMapper.addCollection(userId, recipeId);
        }

    }

    @Override
    public ResponseResult countCollection(Integer id) {
        Integer counter = collectionMapper.countCollection(id);
        if (counter == null) {
            counter = 0;
        }
        return ResponseResult.okResult(counter);
    }

    @Override
    public ResponseResult delete(Integer userId, Integer recipeId) {
        Integer updates = collectionMapper.delete(userId,recipeId);
        if (updates == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult checkCollection(Integer userId, Integer recipeId) {
        Boolean isExist = collectionMapper.checkCollection(userId, recipeId);
        if (isExist){
            throw new SystemException(AppHttpCodeEnum.COLLECTION_IS_EXISTS);
        }else {
            return ResponseResult.okResult();
        }
    }

}
